#define _CRT_SECURE_NO_WARNINGS
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        ListNode* morehead, * moretail;
        ListNode* lesshead, * lesstail;
        morehead = moretail = (ListNode*)malloc(sizeof(ListNode));
        lesshead = lesstail = (ListNode*)malloc(sizeof(ListNode));
        ListNode* pcur = pHead;
        while (pcur)
        {
            if (pcur->val < x)
            {
                lesstail->next = pcur;
                lesstail = lesstail->next;
            }
            else
            {
                moretail->next = pcur;
                moretail = moretail->next;
            }
            pcur = pcur->next;
        }
        lesstail->next = morehead->next;
        moretail->next = NULL;
        return lesshead->next;
    }
};